﻿using System.Threading.Tasks;
using K9Nano.Application;
using K9Nano.Application.MultiTenancy;
using K9Nano.MultiTenancy;
using K9Nano.Share;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace K9Nano.Web.MultiTenancy
{
    [ApiController]
    [Route("tenant/[action]")]
    [Authorize(Policy = K9SharedConstants.PolicyAdmin)]
    public class TenantController : ControllerBase
    {
        protected readonly ITenantAppService TenantAppService;

        public TenantController(ITenantAppService tenantAppService)
        {
            TenantAppService = tenantAppService;
        }

        [HttpPost]
        public async Task<TenantInfo> Create(TenantCreateInput input)
        {
            return await TenantAppService.CreateAsync(input, HttpContext.RequestAborted);
        }

        [HttpDelete]
        public async Task Delete(string id)
        {
            await TenantAppService.DeleteAsync(id, HttpContext.RequestAborted);
        }

        [HttpPut]
        public async Task Update(TenantEditInput input)
        {
            await TenantAppService.UpdateAsync(input, HttpContext.RequestAborted);
        }

        [HttpGet]
        public async Task<PagedResultDto<TenantDto>> GetPagedList(TenantPagedInput input)
        {
            return await TenantAppService.GetPagedListAsync(input, HttpContext.RequestAborted);
        }
    }
}